
//
//  DiscountDetailViewController.m
//  PTravel
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "DiscountDetailViewController.h"
#import "UMSocial.h"
@interface DiscountDetailViewController ()<UMSocialUIDelegate>
@property (retain, nonatomic)UIButton *button;
@end

@implementation DiscountDetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectDiscountSql:@"select * from discount"];
    
    NSMutableArray *array = [NSMutableArray array];
    for (WebDiscount *country in arr) {
        [array addObject:country.titleName];
    }
    if (![array containsObject:self.titleName]) {
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiaoxin1.png"] forState:UIControlStateNormal];
    }else{
        
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiaoxin2.png"] forState:UIControlStateNormal];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *urlStr = [NSString stringWithFormat:@"%@",self.url];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:BOUNDS];
    [self.view addSubview:webView];
    [webView loadRequest:request];
    webView.delegate = self;
    self.juhua.hidesWhenStopped = YES;
    self.navigationItem.title = self.titleName;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(BOUNDS.size.width - 50, 20, 36, 36);
    [self.button addTarget:self action:@selector(collectAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
}
#pragma mark === 收藏按钮
- (void)collectAction
{
    NSString *sql = @"CREATE TABLE discount (url TEXT NOT NULL,titleName TEXT NOT NULL)";
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO discount (url,titleName) VALUES ('%@','%@')",self.url,self.titleName];
    [[DataBaseHandle shareDataBase] createTableSql:sql];
    NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectDiscountSql:@"select * from discount"];
    
    NSMutableArray *array = [NSMutableArray array];
    for (WebDiscount *country in arr) {
        [array addObject:country.titleName];
    }
    if (![array containsObject:self.titleName]) {
        [self.button setBackgroundImage:[UIImage imageNamed:@"iconfont-xiaoxin2.png"] forState:UIControlStateNormal];
        [[DataBaseHandle shareDataBase] insertData:insertSql];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    }else{
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已经收藏过了" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    }
}

- (void)shareAction
{
    NSString *str = [self.url substringToIndex:80];
    NSString *share = [NSString stringWithFormat:@"我在TTravel这个APP上发现了%@%@好划算哦", str, self.titleName];
    
    // 设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:share];
    // 获取标红的位置和长度
    NSRange range = [share rangeOfString:@"TTravel"]; //获取需要变量文字的文字
    [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:20.0]} range:range];
    
    //图片
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, 300)];
    NSURL *url = [NSURL URLWithString:self.photo];
    [image sd_setImageWithURL:url placeholderImage:IMAGE];
    //新浪微博登陆
    
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatform.displayName = @"新浪";
    
    
    UMSocialSnsPlatform *snsPlatform2 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    snsPlatform2.displayName = @"腾讯微博";
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:share
                                     shareImage:image.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
                                       delegate:self];
    
    

}
#pragma mark === 页面开始加载的时候
-(void)webViewDidStartLoad:(UIWebView *)webView{
    
    [self.progress setProgress:0 animated:false];
    [self.juhua startAnimating];
}
#pragma mark === 页面加载结束
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self.progress setProgress:1 animated:YES];
    [self.juhua stopAnimating];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
